<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    /**
     * 当日签到情况
     */
    public function index(){    
    	$vs=M('view_sign');
    	$absent=M('tlsys_absent');
    	$data=$vs->where("class='1704'")->select();

    	//今天凌晨时间戳
    	$todayTime=date("Y-m-d",time());
    	//明天凌晨时间戳
    	$tomorrowTime=date("Y-m-d",strtotime("+1 day"));

    	foreach ($data as $k => $v) {
    		$row=M()->query("select * from tlsys_absent where '{$todayTime}' between startdate and enddate and weixinid='{$v['weixinid']}'");
    		if($row){
    			$data[$k]['absent']=$row[0]['reason'];
    		}else{
    			$data[$k]['absent']='';
    		}
    	}
    	$this->assign('now',time());
    	$this->assign('sign',$data);
        $this->display();
    }
    /**
     * 某日签到情况查看
     */
     public function weekSign($gclass='1704'){
    	$MStudent=M('student');
    	$absent=M('tlsys_absent');
    	$data=$MStudent->field('id,weixinid,stuname,class')->where(" isdel=0 and class='{$gclass}'")->select();
    	//七日前日期
    	$days7before=date("Y-m-d",strtotime("-7 days"));
    	// foreach ($data as $k=>$v) {
    	// 	for ($i=7; $i>=0 ; $i--) { 
	    // 		$day=date("Y-m-d",strtotime("-$i days"));
	    // 		$dayTamp=strtotime($day);
	    // 		// echo $day."&nbsp;";
	    // 		$dayNext=date("Y-m-d",strtotime("$day +1 days"));
	    // 		// echo $dayNext."&nbsp;";
	    // 		$dayNextTamp=strtotime($dayNext);

	    // 		// 取改日期的签到信息
	    // 		$sql="select signintime from tlsys_sign where weixinid='{$data[$k]['weixinid']}' and signintime between {$dayTamp} and {$dayNextTamp}";

	    // 		$sign=M()->query($sql);
	    // 		// echo $sql."<hr>";
	    // 		if($sign){
	    // 			$data[$k][$day]=$sign[0]['signintime'];
	    // 		}else{
	    // 			$data[$k][$day]='';
	    // 		}
	    // 	}
    	// }
    	$this->assign('now',time());
    	$this->assign('stu',$data);
        $this->display();
    }

    public function getStu($gclass='1704'){
        $MStudent=M('student');
        $data=$MStudent->field('id,weixinid,stuname,class')->where(" isdel=0 and class='{$gclass}'")->select();
        $this->ajaxReturn($data);
    }

    public function weekSignSearch($gclass='1704',$name='',$date=''){
        if(empty($date)){
            $date=date('Y-m-d',time());
        }
        //选中那天的0晨时间戳
        $dayStamp=strtotime($date);
        //选中那天的下一天的0晨时间戳
        $tomStamp=strtotime(date('Y-m-d',strtotime("$date +1 day")));
        

        $sql="SELECT s.id, s.stuname, s.class, s.weixinid, ts.signintime,ts.signouttime";
        $sql.=" FROM student s LEFT OUTER JOIN tlsys_sign ts ON s.weixinid = ts.weixinid";
        $sql.=" AND ts.signintime BETWEEN {$dayStamp} AND {$tomStamp} ";
        $sql.=" where isdel=0 and  s.class='{$gclass}'";
        if(!empty($name)){
            $sql.=" and s.stuname='{$name}'";
        }
        // echo $sql;
        $data=M()->query($sql);

        foreach ($data as $k => $v) {
            $data[$k]['signintime']=empty($v['signintime'])?'':date('Y-m-d H:i:s',$v['signintime']);
            $data[$k]['signouttime']=empty($v['signouttime'])?'':date('Y-m-d H:i:s',$v['signouttime']);

            $row=M()->query("select * from tlsys_absent where '{$date}' between startdate and enddate and weixinid='{$v['weixinid']}'");
            if($row){
                $data[$k]['absent']=$row[0]['reason'];
            }else{
                $data[$k]['absent']='';
            }
        }
        $this->ajaxReturn($data);
    }

    /**
     * 统计页面
     */
    public function statistic(){
        $this->assign('now',time());
        $this->display();
    }
    public function statisticSearch($gclass='1704',$datebegin='',$dateend=''){
        $stu=M('student')->field('id,stuname,weixinid')->where(" isdel=0 and class='{$gclass}'")->select();
        $begin=strtotime($datebegin);
        $end=strtotime(date('Y-m-d',strtotime("$dateend +1 day")));

        foreach ($stu as $k=>$v) {
            //该段日期内的请假次数
            $absentData=M('tlsys_absent')->where("weixinid='{$v['weixinid']}' and startdate between '{$datebegin}' and '{$dateend}'")->select();
            $absentCount=count($absentData);
            $stu[$k]['absents']=$absentCount;
            $stu[$k]['absentsdata']=$absentData;

            //该段日期内的迟到次数
            $delayData=M('tlsys_delay')->where("weixinid='{$v['weixinid']}' and signintime between {$begin} and {$end}")->select();
            $delayCount=count($delayData);
            $stu[$k]['delays']=$delayCount;
            $stu[$k]['delaysdata']=$delayData;

            //该段日期内的早退次数
            $earlyquitData=M('tlsys_early_quit')->where("weixinid='{$v['weixinid']}' and signouttime between {$begin} and {$end}")->select();
            $earlyquitCount=count($earlyquitData);
            $stu[$k]['earlyquit']=$earlyquitCount;
            $stu[$k]['earlyquitdata']=$earlyquitData;

            //该段日期内的旷课次数
            $skips=$this->skipStatics($v['weixinid'],$datebegin,$dateend);
            $stu[$k]['skip']=$skips['skipcount'];
            $stu[$k]['skipdata']=$skips['skipdata'];
        }
        $this->ajaxReturn($stu);
    }

    /**
     * 统计旷课次数
     */
    public function skipStatics($weixinid='',$datebegin='',$dateend=''){
        $count=0;
        $skipDays=array();

        $begin=strtotime($datebegin);
        $end=strtotime(date('Y-m-d',strtotime("$dateend +1 day")));


        //该签到的日期
        $signDays=$this->signDay($begin,$end);

        // p($signDays);
        // exit;
        
        while (strtotime($datebegin)<=strtotime($dateend)) {
            //当天的0晨时间戳
            $dateBeginStamp=strtotime($datebegin);
            // 第二天的0晨时间戳
            $dateTomorrowStamp=strtotime(date('Y-m-d',strtotime("$datebegin +1 day")));
            //不是假期再处理
            if(in_array($datebegin,$signDays)){
                
                //该日内是否有签到
                $signCount=M("tlsys_sign")->where("weixinid='{$weixinid}' and signintime between {$dateBeginStamp} and {$dateTomorrowStamp}")->count();
                //该日内是否有请假
                $sqlAbsent="select count(id) as c from tlsys_absent where weixinid='{$weixinid}' and '{$datebegin}' between startdate and enddate";
                // echo $sqlAbsent."<br/>";
                $absentResult=M()->query($sqlAbsent);
                $absentCount=$absentResult[0]['c'];
                // p($absentCount);


                //如果有一个存在，说明没旷课,都不存在则为旷课
                if($signCount==0 and $absentCount==0){
                   $count++; 
                   $skipDays[]=$datebegin;
                }
            }
            //日期+1
            $tmpDate=date('Y-m-d',strtotime("$datebegin +1 day"));
            $datebegin=$tmpDate;
        }
        return array('skipcount'=>$count,'skipdata'=>$skipDays);
    }
    /**
    * 得到签到的日期
    */
    public function signDay($dateBeginStamp,$dateEndStamp){
        $sql="select FROM_UNIXTIME(signintime,'%Y-%m-%d') as days from tlsys_sign where signintime between {$dateBeginStamp} and {$dateEndStamp} group by (FROM_UNIXTIME(signintime,'%Y%m%d')) order by signintime desc";
        // echo $sql;
        // exit;
        $signDays=M()->query($sql);
        // 将结果的二维数组转成一维数组，以便通过in_array()进行判断
        $signs=array();
        foreach ($signDays as $k=>$v) {
            $signs[$k]=$v['days'];

        }
        return $signs;
    }   
    public function test(){
    	 //本周开始日期

		// echo date("Y-m-d",strtotime("-7 days")), "\n";
		// echo date("Ymd",strtotime("+0 week Monday")), "\n";
        
        $cbt=getConfig('class_begin_time');
        $classBeginTime=date('Y-m-d',time()).' '.$cbt;
        echo $classBeginTime;
    }
}